package reduce.fz.com.reduce.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.alibaba.fastjson.TypeReference;

import java.io.UnsupportedEncodingException;
import java.util.List;

import reduce.fz.com.reduce.R;
import reduce.fz.com.reduce.abstracts.AbResponseListener;
import reduce.fz.com.reduce.activity.UserDetailActivity;
import reduce.fz.com.reduce.adapter.BaseRecycleViewHolder;
import reduce.fz.com.reduce.bean.ServiceException;
import reduce.fz.com.reduce.bean.Society;
import reduce.fz.com.reduce.bean.User;
import reduce.fz.com.reduce.bean.page.Pagination;
import reduce.fz.com.reduce.bean.result.Result;
import reduce.fz.com.reduce.custom.view.RoundNetworkImageView;
import reduce.fz.com.reduce.fragment.base.BaseListFragment;
import reduce.fz.com.reduce.util.ApiUtil;
import reduce.fz.com.reduce.util.HttpUtils;

/**
 */
public class UserListFragment extends BaseListFragment<UserListFragment.UserViewHolder, User> {
    public String keyword;
    //type=0根据关键字查找 1 根据标签id查找
    public int type = 0;
    public Long tagid;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt("type");
        tagid = getArguments().getLong("tagid");
    }

    public UserListFragment() {
    }

    public static UserListFragment createSearch() {
        UserListFragment fragment = new UserListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("type", 0);
        fragment.setArguments(bundle);
        return fragment;
    }

    public static UserListFragment createTag(Long tagid) {
        UserListFragment fragment = new UserListFragment();
        Bundle bundle = new Bundle();
        bundle.putLong("tagid", tagid);
        bundle.putInt("type", 1);
        fragment.setArguments(bundle);
        return fragment;
    }


    public void searchUser(String info) {
        try {
            keyword = java.net.URLEncoder.encode(info, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        firstPage();
    }

    @Override
    public LinearLayoutManager getLayoutManager() {
        return null;
    }

    /**
     * 跳转到用户详情页面
     *
     * @param uid
     */
    public void forwardUserDetail(Long uid) {
        Intent intent = new Intent(getActivity(), UserDetailActivity.class);
        intent.putExtra("uid", uid);
        startActivity(intent);

    }

    @Override
    protected UserViewHolder createItemViewHolder(ViewGroup parent) {
        View view = layoutInflater.inflate(R.layout.view_society_memrank, parent, false);
        UserViewHolder holder = new UserViewHolder(view, new UserClickListener() {
            @Override
            public void memberClick(User user) {
                forwardUserDetail(user.uid);
            }
        });
        return null;
    }

    @Override
    public void request(String url) {
        HttpUtils.get(url, new AbResponseListener<List<User>>(new TypeReference<Result<List<User>>>() {
        }) {
            @Override
            public void error(ServiceException e) {
                addResult(null);

            }

            @Override
            public void responseInfo(Result<List<User>> data) {
                addResult(data);
            }
        });
    }



    @Override
    protected UserViewHolder createfootViewHolder(View view) {
        return new UserViewHolder(view);
    }

    @Override
    protected String setUrlBeforeRequest(List<User> datas, Pagination pagination) {
        String url = "";
        switch (type) {
            case 0:
                url = ApiUtil.getApi(context, R.array.api_search_users_keyword, pagination.getPageNo(), pagination.getPageSize());
                url = url + "&keyword=" + keyword;
                break;
            case 1:
                url = ApiUtil.getApi(context, R.array.api_search_users_tag, tagid, pagination.getPageNo(), pagination.getPageSize());
                break;
            default:
                break;
        }
        return url;
    }


    @Override
    protected int getPageType() {
        return 0;
    }

    public interface UserClickListener {
        public void memberClick(User user);
    }

    public static class UserViewHolder extends BaseRecycleViewHolder<User> {
        private RoundNetworkImageView mAtavar;
        private TextView mNickName;
        private TextView mRank;
        private TextView mWorks;
        private TextView mFollow;
        private UserClickListener listener;
        private View itemView;

        public UserViewHolder(View itemView) {
            super(itemView);
            initWidget(itemView);
        }


        public UserViewHolder(View itemView, UserClickListener listener) {
            this(itemView);
            this.listener = listener;

        }

        public void initWidget(View view) {
            this.itemView = view;
            mAtavar = (RoundNetworkImageView) view.findViewById(R.id.iv_user_avatar);
            mNickName = (TextView) view.findViewById(R.id.tv_user_nickname);
            mRank = (TextView) view.findViewById(R.id.tv_rank);
            mWorks = (TextView) view.findViewById(R.id.tv_works);
            mFollow = (TextView) view.findViewById(R.id.tv_follow);
        }

        @Override
        public void renderView(final User data, int position) {

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.memberClick(data);
                }
            });
            mAtavar.setImageUrl(data.avatar, HttpUtils.getAvatarImageLoader());
            mNickName.setText(data.nickname);
            mWorks.setText(data.userAttach.sign_num + "");
            mFollow.setText(data.userAttach.follow_num + "");

        }
    }


}
